initEditor();
axios.get('http://www.itcbc.com:8080/my/category/list',{
    headers: {
        Authorization: localStorage.getItem('token')
    }
}).then(res => {
    let {status,data} = res.data;
    if (status === 0) {
        let str = '';
        data.forEach(item => {
            str += `<option value="${item.id}">${item.name}</option>`;
        });
        $('select[name=cate_id]').append(str);
        let form = layui.form;
        form.render('select');
    }
})
let image = $('#image');
let options = {
    aspectRatio: 400/280,
    preview: '.img-preview'
}
image.cropper(options);
$('button:contains("选择封面")').click(function() {
    $('#file').trigger('click');
});
$('#file').change(function() {
    if(this.files.length>0) {
        let fileObj = this.files[0];
        let url = URL.createObjectURL(fileObj);
        image.cropper('replace',url);
    }
})
$('form').on('submit',function(e) {
    e.preventDefault();
    let fd = new FormData(this);
    // fd.forEach((item,index) => console.log(index,item))
    // 获取tinyMCE编辑器中的内容方法：tinyMCE.activeEditor.getContent()
    fd.set('content',tinyMCE.activeEditor.getContent());
    let canvas = image.cropper('getCroppedCanvas',{width:400,height:280});
    canvas.toBlob(function(blob) {
        fd.append('cover_img',blob);
        // 打印查看拿到所有接口文档需要的参数了
        // fd.forEach((item,index) => console.log(index,item))
        axios.post('http://www.itcbc.com:8080/my/article/add',fd,{
            headers: {
                Authorization: localStorage.getItem('token')
            }
        }).then(res => {
            let {status,message} = res.data;
            if(status === 0) {
                layer.msg(message)
            }
        })
    })
})